package com.woniu.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.account.dao.TPointMapper;
import com.woniu.account.model.TPoint;
import com.woniu.account.service.TPointService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2024年03月01日
 */
@Service
public class TPointServiceImpl extends ServiceImpl<TPointMapper, TPoint> implements TPointService {
    @Resource
    private TPointMapper tPointMapper;
    @Override
    public List<TPoint> search() {
        QueryWrapper<TPoint> wrapper = new QueryWrapper<TPoint>();
        List<TPoint> tPoints = tPointMapper.selectList(wrapper);

        return tPoints;
    }

    @Override
    public TPoint insert(String pLevel, Double pMoney, Double pDiscount) throws Exception {
        QueryWrapper<TPoint> wrapper = new QueryWrapper<>();
        List<TPoint> tPoints = tPointMapper.selectList(wrapper);
        int size = tPoints.size();
        TPoint tPoint =null;
        if(size>0){
            tPoint = tPoints.get(size - 1);
        }
        // pMoney 小于 tPoint.getPMoney()
        if (BigDecimal.valueOf(pMoney).compareTo(tPoint.getPMoney()) <= 0) {
            throw new Exception("金额不能小于等于上一等级");
        }
        // pMoney 小于 tPoint.getPMoney()
        if (BigDecimal.valueOf(pDiscount).compareTo(tPoint.getPDiscount()) >= 0) {
            throw new Exception("折扣率不能大于等于上一等级");
        }
        TPoint point = new TPoint();
        point.setPLevel(pLevel);
        point.setPMoney(BigDecimal.valueOf(pMoney));
        point.setPDiscount(BigDecimal.valueOf(pDiscount));
        tPointMapper.insert(point);
        return point;
    }

    @Override
    public TPoint modify(Integer pointId,Double pMoney, Double pDiscount) throws Exception {
        List<TPoint> tPoints = tPointMapper.selectList(new QueryWrapper<TPoint>().orderByAsc("point_id"));

// 找到目标对象的索引
        int index = -1;
        for (int i = 0; i < tPoints.size(); i++) {
            if (tPoints.get(i).getPointId() == pointId) {
                index = i;
                break;
            }
        }
        //找到当前等级的下一等级
        if (index != -1 && index + 1 < tPoints.size()) {
            // 现在是修改对象的下一等级
            TPoint point = tPoints.get(index + 1);
            if(BigDecimal.valueOf(pMoney).compareTo(point.getPMoney()) >= 0||BigDecimal.valueOf(pDiscount).compareTo(point.getPDiscount()) <= 0){
                throw new Exception("金额不能大于下一等级或折扣率不能小于下一等级");
            }

        }
        //找到当前等级的上一等级
        if (index != -1 && index -1 >=0) {
            // 现在是修改对象的下一等级
            TPoint point = tPoints.get(index - 1);
            if(BigDecimal.valueOf(pMoney).compareTo(point.getPMoney()) <=0||BigDecimal.valueOf(pDiscount).compareTo(point.getPDiscount()) >= 0){
                throw new Exception("金额不能小于上一等级或折扣率不能大于上一等级");
            }

        }
        TPoint point = tPointMapper.selectById(pointId);
        point.setPMoney(BigDecimal.valueOf(pMoney));
        point.setPDiscount(BigDecimal.valueOf(pDiscount));
        tPointMapper.updateById(point);
        return point;
    }
}
